const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    oldPassword: '',
    newPassword: '',
    rePassword: ''
  },
  onLoad(options) {

  },
  getInputValue(e) {
    const item = e.currentTarget.dataset.item
    this.setData({
      [item]: e.detail.value
    })
  },
  check() {
    let errorMsg = '',
      flag = false;
    if (!(this.data.newPassword === this.data.rePassword)) {
      errorMsg = '输入新密码和确认密码不匹配'
    } else {
      flag = true
    }
    if (!flag) {
      wx.showToast({
        title: errorMsg,
        icon: 'none'
      })
    }

    return flag;
  },
  noVacancy() {
    if (!(this.data.oldPassword && this.data.rePassword && this.data.rePassword)) {
      return false
    } else {
      return true
    }
  },
  doChange() {
    if (!this.noVacancy()) {
      return false
    }
    if (this.check()) {
      app.fetchRequest({
        url: '/user/update',
        data: {
          "oldPassword": this.data.oldPassword,
          "newPassword": this.data.newPassword,
        },
        success: (res) => {
          wx.switchTab({
            url: '../user/user',
          })
        },
        fail: (msg) => {
          wx.showToast({
            title: msg,
            icon: 'none'
          })
        }
      })
    }
  }
})